<?php

class SiteController extends ExtController
{

    /**
     * This function is a delegate method called from ExtYiiModelReader
     * in order to configure the reader columns of the store
     * @param <type> $field
     * @param <type> $metadata
     */
    public function EmployeeColumnDelegate($field, $metadata)
    {
        if ($metadata->dbType == 'datetime')
        {
            $field->setType("date");
            // convert from mysql date to js
            $field->setConvert('function(v) { return Date.parseDate(v,"c").clearTime(); }');
            $field->setDateFormat('Y/m/d');
        }
    }

    /**
     * This function create URL templates to be used from client-side
     */
    public function createURLTemplates()
    {
        $templates = array();
        $arguments = array();
        for ($a = 0; $a < 5; $a++)
        {
            $templates['action' . $a] = $this->createUrl('_controller_/_action_', $arguments);
            $arguments['arg' . $a] = 'value' . $a;
        }
        return CJSON::encode($templates);
    }

    public function actionDashboard()
    {
        $items = array(
            array(
                'title' => 'Customers and Invoices',
                'items' => array(
                    array(
                        'title' => 'Customers',
                        'icon' => Yii::app()->request->baseUrl . '/images/icon.gif'),
                    array(
                        'title' => 'Invoices',
                        'icon' => Yii::app()->request->baseUrl . '/images/icon.gif'),
                ),
            ),
            array(
                'title' => 'Artists and Tracks',
                'items' => array(
                    array(
                        'title' => 'Artists',
                        'icon' => Yii::app()->request->baseUrl . '/images/icon.gif'),
                    array(
                        'title' => 'Tracks',
                        'icon' => Yii::app()->request->baseUrl . '/images/icon.gif'),
                ),
            ),
            array(
                'title' => 'Back Office',
                'items' => array(
                    array(
                        'title' => 'Sales Representitives',
                        'icon' => Yii::app()->request->baseUrl . '/images/icon.gif')
                ),
            )
        );
        echo $this->ExportToJSONDataSource($items);
    }

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionInvoices()
    {
        $this->render('invoices');
    }

    public function actionCustomers()
    {
        $this->render('customers');
    }

    public function actionEmployees()
    {
        $this->render('employees');
    }

    public function actionError()
    {
        /**
         * ExtJS normally resets all the CSS' but for the errors
         * we will disable that to get a more readable error page
         */
        Yii::app()->ext4yii->disable_css_reset = true;

        if ($error = Yii::app()->errorHandler->error)
        {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

    public function filters()
    {
        return array('accessControl');
    }

    public function accessRules()
    {
        return array(
            array('allow', 'users' => array('@'),),
            array('deny', 'users' => array('*')),
        );
    }

}